package com.simon.mctm.biz.sys.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import com.simon.mctm.base.response.DoResponse;
import com.simon.mctm.base.response.PageResponse;
import com.simon.mctm.exception.CheckException;
import com.simon.mctm.exception.OperationFailedException;
import com.simon.mctm.token.TokenValidate;
import com.simon.mctm.base.PageResult;
import com.simon.mctm.biz.sys.bean.Dict;
import com.simon.mctm.biz.sys.service.DictService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;


/**
 * <p>
 * 业务字典 前端控制器
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@RestController
@Api(description = "业务字典控制器")
@RequestMapping("/sys/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "添加业务字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "dict", value = "业务字典对象", required = true, dataType = "Dict")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addDict(@RequestBody Dict dict) {
        Dict temp = dictService.selectOne(
                new EntityWrapper<Dict>().eq("code", dict.getCode())
                        .eq("pid", dict.getPid())
        );
        if (temp != null) {
            throw new OperationFailedException("该业务字典已经存在");
        }
        boolean res = dictService.insert(dict);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException("添加业务字典失败");
        }
    }


    @ApiOperation(value = "获取指定业务字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "业务字典id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getDict(@RequestParam Integer id) {
        Dict dict = dictService.selectById(id);
        if (dict != null) {
            return new DoResponse<>(dict);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取业务字典列表")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getAllDict() {
        EntityWrapper<Dict> ew = new EntityWrapper<>();
        List<Dict> dictList = dictService.selectList(ew);
        if (!CollectionUtils.isEmpty(dictList)) {
            return new DoResponse<>(dictList);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "查询业务字典列表,分页形式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", defaultValue = "10")
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @TokenValidate
    public PageResponse<?> getDictPagedList(@RequestParam String code,@RequestParam String name,@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        if (pageNum == 0 || pageSize == 0) {
            throw new CheckException("页码和单页条目必输");
        }
        EntityWrapper<Dict> ew = new EntityWrapper<>();
        Page<Dict> dictPage = dictService.selectPage(
                new Page<>(pageNum, pageSize),
                new EntityWrapper<Dict>().like("code",code)
                .like("name",name)
        );
        List<Dict> dictList = dictPage.getRecords();
        PageResult bPage = dictService.calPage(code,name,pageSize);
        if (!CollectionUtils.isEmpty(dictList)) {
            return new PageResponse<>(dictList, dictList.size(), bPage.getTotalElements(), bPage.getTotalPage());
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "删除业务字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "业务字典id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteDict(@RequestParam Integer id) {
        boolean res = dictService.deleteById(id);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "更新业务字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "业务字典id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "dict", value = "业务字典对象", required = true, dataType = "Dict")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateDict(@RequestParam Integer id, @RequestBody Dict dict) {
        Dict temp = dictService.selectById(id);
        if (temp == null) {
            throw new OperationFailedException("该业务字典不存在");
        }
        Dict temp1 = dictService.selectOne(
                new EntityWrapper<Dict>().eq("code", dict.getCode())
                        .eq("pid", dict.getPid())
        );
        if (temp1 != null) {
            throw new OperationFailedException("该业务字典已经存在,编码不允许重复");
        }
        boolean res = dictService.updateById(dict);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "查询子字典值", notes = "根据code获取子字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "上级字典code", required = true, dataType = "String")
    })
    @RequestMapping(value = "/sublist", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> sublistDict(@RequestParam String code) {
        List<Dict> dictList = dictService.selectList(
                new EntityWrapper<Dict>().eq("pcode",code)
        );
        if (dictList != null) {
            return new DoResponse<>(dictList);
        } else {
            throw new OperationFailedException();
        }
    }
}

